package features;

import java.security.Provider;

import gui.OrderListActivity;
import agent.AgentManager;
import agent.behaviours.SendExpertLocationPeriodicallyBehaviour;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;

import com.mechanic.androidexpert.R;

public class LocationService extends Service {

	private static final long LOCATION_REFRESH_TIME = 0;

	private static final float LOCATION_REFRESH_DISTANCE = 0;

	private final int TIME_PERIOD = 10000;

	private static int classID = 699;
	private SendExpertLocationPeriodicallyBehaviour behaviour;

	LocationManager mLocationManager;

	private final LocationListener locationListener = new LocationListener() {

		@Override
		public void onLocationChanged(final Location location) {
			System.out.println("location changed");

			if (AgentManager.getAgent() == null
					|| AgentManager.getAgent().getExpertData() == null)
				return;

				AgentManager
						.getAgent()
						.getExpertData()
						.setLocation(
								new structures.Location(location.getLatitude(),
										location.getLongitude()));

		}

		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onProviderEnabled(String provider) {
			System.out.println("onProviderEnabled");

		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			System.out.println("onStatusChanged");

		}
	};

	@Override
	public void onCreate() {
		behaviour = new SendExpertLocationPeriodicallyBehaviour(
				AgentManager.getAgent(), TIME_PERIOD);
		AgentManager.getAgent().addBehaviour(behaviour);

		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

		mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
				LOCATION_REFRESH_TIME, LOCATION_REFRESH_DISTANCE,
				locationListener);

		Location location = mLocationManager
				.getLastKnownLocation(LocationManager.GPS_PROVIDER);

		if (location != null)
			AgentManager
					.getAgent()
					.getExpertData()
					.setLocation(
							new structures.Location(location.getLatitude(),
									location.getLongitude()));

		super.onCreate();
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		// notyfikacja:
		Intent i = new Intent(this, OrderListActivity.class);
		i.setAction("com.location");

		NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		PendingIntent pi = PendingIntent.getActivity(this, 0, i,
				PendingIntent.FLAG_UPDATE_CURRENT);

		String body = "Location service is running.";
		String title = "ExperApp - locating";

		Notification n = new Notification(R.drawable.localization_notification, title, 0);
		n.setLatestEventInfo(this, title, body, pi);

		stopForeground(true);
		startForeground(classID, n);

		return Service.START_STICKY;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		mLocationManager.removeUpdates(locationListener);
		stopForeground(true);

	}

	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
